package com.c2b.algorithm.leetcode.base;

import java.util.HashSet;
import java.util.Set;

/**
 * <a href='https://leetcode.cn/problems/unique-morse-code-words/'>唯一摩尔斯密码词(Unique Morse Code Words)</a>
 * <p>
 * 国际摩尔斯密码定义一种标准编码方式，将每个字母对应于一个由一系列点和短线组成的字符串， 比如:
 *     <ul>
 *         <li>'a' 对应 ".-" ，</li>
 *         <li>'b' 对应 "-..." ，</li>
 *         <li>'c' 对应 "-.-." ，以此类推。</li>
 *     </ul>
 *     为了方便，所有 26 个英文字母的摩尔斯密码表如下：[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
 * </p>
 * <p>
 *     给你一个字符串数组 words ，每个单词可以写成每个字母对应摩尔斯密码的组合。
 *     <ul>
 *         <li>例如，"cab" 可以写成 "-.-..--..." ，(即 "-.-." + ".-" + "-..." 字符串的结合)。我们将这样一个连接过程称作 单词翻译 。</li>
 *     </ul>
 *
 * </p>
 * <p>对 words 中所有单词进行单词翻译，返回不同 单词翻译 的数量。</p>
 *
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *      输入: words = ["gin", "zen", "gig", "msg"]
 *      输出: 2
 *      解释:
 *          各单词翻译如下:
 *          "gin" -> "--...-."
 *          "zen" -> "--...-."
 *          "gig" -> "--...--."
 *          "msg" -> "--...--."
 *          共有 2 种不同翻译, "--...-." 和 "--...--.".
 *
 * 示例 2：
 *      输入：words = ["a"]
 *      输出：1
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 *     <ul>
 *         <li>1 <= jewels.length, stones.length <= 50</li>
 *         <li>jewels 和 stones 仅由英文字母组成</li>
 *         <li>jewels 中的所有字符都是 唯一的</li>
 *     </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/12/18 14:28
 */
public class LC0804UniqueMorseCodeWords_S {

    static class Solution {
        public int uniqueMorseRepresentations(String[] words) {
            String[] map = new String[]{
                    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
            };
            Set<String> uniqueMorseRepresentations = new HashSet<>();
            for (String word : words) {
                char[] chars = word.toCharArray();
                StringBuilder stringBuilder = new StringBuilder();
                for (char ch : chars) {
                    stringBuilder.append(map[ch - 'a']);
                }
                uniqueMorseRepresentations.add(stringBuilder.toString());
            }
            return uniqueMorseRepresentations.size();
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"}));
    }
}
